import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class AdminRegisterPanel extends JPanel{
       
        JLabel fn=new JLabel("Term: ");
    JLabel ln=new JLabel("Course Name: ");
    JLabel id=new JLabel("Course #: ");
    JTextField termText=new JTextField(10);
    JTextField nameText=new JTextField(13);
    JTextField numberText=new JTextField(10);
        JPanel panel = new JPanel();
        JPanel panelt = new JPanel();
        JPanel panelc = new JPanel();
        JPanel panelc1 = new JPanel();
        JPanel panelc2 = new JPanel();
        JPanel panelb = new JPanel();
        JButton delete=new JButton();
        JButton home=new JButton();
        JButton add=new JButton();
        JButton search=new JButton();
        JButton reset=new JButton();
        JTable table;
        JTable table2;
        String[] cols={"Delete","Term", "Course Title","Course Number", "Professor", "Meeting Info", "Credits"};
        String[] cols2={"Term", "Course Title","Course Number", "Professor", "Day","Start Time","End Time", "Credits"};
        Boolean j=false;
        Object data[][]={};
        Object data2[][]={{"","","","","","","",""},
                                        {"","","","","","","",""},
                                        {"","","","","","","",""},
                                        {"","","","","","","",""},
                                        {"","","","","","","",""}};
        DefaultTableModel model = new DefaultTableModel(null, cols){
        public Class getColumnClass(int c) {
            switch (c) {
              case 0: return Boolean.class;
              default: return String.class;
            }  
        }
        };
       
        DefaultTableModel model2 = new DefaultTableModel(null, cols2){};
        DefaultTableModel dtm;
        int row=0,crow=0;
        Image img;
       
        public void searchActionPerformed(ActionEvent e){
                String s_term = termText.getText();
                String s_name = nameText.getText();
                String s_number = numberText.getText();
                

                
                if ((s_term.length()>0) && (s_name.length()>0) && (s_number.length()>0))
                {
                try{
                // Clear the table
                	for(int i =0; i<dtm.getRowCount(); i++){
                        dtm.removeRow(i);
                        i--;  
	}                    
                        
                String dName = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                Connection con = DriverManager.getConnection(url);
                java.sql.Statement mysqlStatement = con.createStatement();
                java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT cu.term, cu.start_time, cu.end_time, cu.days, cu.instructor, ca.course_name, ca.course_number, ca.credit_hours, cu.CID FROM next_semester_course_list AS cu JOIN course_catalog AS ca ON cu.CID = ca.CID WHERE (cu.term='"+s_term+"') AND (ca.course_name='"+s_name+"') AND (ca.course_number='"+s_number+"') GROUP BY course_name");
               
                while (resultSet.next()) {
                        row+=1;
                        String term=resultSet.getString(1);
                        String start_time="";//=resultSet.getString(2);
                        String end_time="";//=resultSet.getString(3);
                        //String days;//=resultSet.getString(4);
                        String instructor=resultSet.getString(5);
                        String course_name=resultSet.getString(6);
                        String course_number=resultSet.getString(7);
                       
                        int credit_hours=resultSet.getInt(8);
                        String cid=resultSet.getString(9);
                        String days="";
                       
                        try{
                                String dName2 = "com.mysql.jdbc.Driver";
                                String url2 = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                                Connection con2 = DriverManager.getConnection(url2);
                                java.sql.Statement mysqlStatement2 = con2.createStatement();
                                //java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT course_name, start_time, days From course_catalog, current_course_total_list WHERE course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
                                java.sql.ResultSet resultSet2 = mysqlStatement2.executeQuery("SELECT co.start_time, co.end_time, co.days, co.CID From course_catalog AS ca JOIN next_semester_course_list AS co ON ca.CID = co.CID WHERE ca.CID ='"+cid+"' AND co.SID='"+MenuPanel.SID+"'");//start_time='"+start_time+"'");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
                               
                                //while (resultSet.next()&&resultSet2.next()) {
                                        while(resultSet2.next()){
                                        //String course_number= resultSet.getString(1);
                                        //String course_section=resultSet.getString(3);
                                        //course=course_number+"-"+course_name+"-"+course_section;
                                       
                                        String startTime=resultSet2.getString(1);
                                        String endTime=resultSet2.getString(2);
                                        String day=resultSet2.getString(3);
                                        start_time=startTime;
                                        end_time=endTime;
                                        crow+=1;
                                        //start_time+=startTime;
                                        //end_time+=endTime;
                                        days+=day;
                                       
                                        System.out.println(cid+": "+startTime+ " "+endTime+" "+days+" ");
                                       
                                       
                                }
                                con2.close();
                                }
                                catch(Exception ex){}
                        String dd=days+" "+start_time+":00 - "+end_time+":00";
                       
                        Object[] program={j,term,course_name,course_number,instructor,dd,credit_hours};
                        dtm.addRow(program);
                        System.out.println("added");
                }
                con.close();
                }
                catch(Exception ex){}
                }
                else {
                        JOptionPane.showMessageDialog(null, "All fields required!");
                }
        }
        
        public void resetActionPerformed(ActionEvent e){
        	for(int i =0; i<dtm.getRowCount(); i++){
                dtm.removeRow(i);
                i--;  
}
                                
                try{
                String dName = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                Connection con = DriverManager.getConnection(url);
                java.sql.Statement mysqlStatement = con.createStatement();
                java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT cu.term, cu.start_time, cu.end_time, cu.days, cu.instructor, ca.course_name, ca.course_number, ca.credit_hours, cu.CID FROM next_semester_course_list AS cu JOIN course_catalog AS ca ON cu.CID = ca.CID  GROUP BY course_name");
               
                while (resultSet.next()) {
                        row+=1;
                        String term=resultSet.getString(1);
                        String start_time="";//=resultSet.getString(2);
                        String end_time="";//=resultSet.getString(3);
                        //String days;//=resultSet.getString(4);
                        String instructor=resultSet.getString(5);
                        String course_name=resultSet.getString(6);
                        String course_number=resultSet.getString(7);
                       
                        int credit_hours=resultSet.getInt(8);
                        String cid=resultSet.getString(9);
                        String days="";
                       
                        try{
                                String dName2 = "com.mysql.jdbc.Driver";
                                String url2 = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                                Connection con2 = DriverManager.getConnection(url2);
                                java.sql.Statement mysqlStatement2 = con2.createStatement();
                                //java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT course_name, start_time, days From course_catalog, current_course_total_list WHERE course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
                                java.sql.ResultSet resultSet2 = mysqlStatement2.executeQuery("SELECT co.start_time, co.end_time, co.days, co.CID From course_catalog AS ca JOIN next_semester_course_list AS co ON ca.CID = co.CID WHERE ca.CID ='"+cid+"' AND co.SID='"+MenuPanel.SID+"'");//start_time='"+start_time+"'");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
                               
                                //while (resultSet.next()&&resultSet2.next()) {
                                        while(resultSet2.next()){
                                        //String course_number= resultSet.getString(1);
                                        //String course_section=resultSet.getString(3);
                                        //course=course_number+"-"+course_name+"-"+course_section;
                                       
                                        String startTime=resultSet2.getString(1);
                                        String endTime=resultSet2.getString(2);
                                        String day=resultSet2.getString(3);
                                        start_time=startTime;
                                        end_time=endTime;
                                        crow+=1;
                                        //start_time+=startTime;
                                        //end_time+=endTime;
                                        days+=day;
                                       
                                        System.out.println(cid+": "+startTime+ " "+endTime+" "+days+" ");
                                       
                                       
                                }
                                con2.close();
                                }
                                catch(Exception ex){}
                        String dd=days+" "+start_time+":00 - "+end_time+":00";
                       
                        Object[] program={j,term,course_name,course_number,instructor,dd,credit_hours};
                        dtm.addRow(program);
                        System.out.println("added");
                }
                con.close();
                }
                catch(Exception ex){}
                        }
        
        public AdminRegisterPanel(){
     		final ImageIcon re=new ImageIcon("reset_button.png");
     		final ImageIcon cre=new ImageIcon("reset_cbutton.png");
     		final ImageIcon se=new ImageIcon("search_button.png");
     		final ImageIcon cse=new ImageIcon("search_cbutton.png");
     		button(search,se);
     		button(reset,re);
               
                        search.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                                searchActionPerformed(e);
                }
                        });
                        
                        search.setContentAreaFilled(false);
                        search.setIcon(se);
                        search.setRolloverIcon(cse);
                        search.setPressedIcon(cse);
                        search.getModel().addChangeListener(new ChangeListener() {
                		        @Override
                		        public void stateChanged(ChangeEvent e) {
                		            ButtonModel model = (ButtonModel) e.getSource();
                		            if (model.isRollover()) {
                		            	search.setIcon(cse);
                		            } else {
                		            	search.setIcon(se);
                		            }// etc ...//  if (model.isPressed()) {
                		         }
                		    });
                        
                        reset.addActionListener(new ActionListener(){
                                public void actionPerformed(ActionEvent e){
                                        resetActionPerformed(e);
                                }
                        });
                
                        reset.setContentAreaFilled(false);
                        reset.setIcon(re);
                        reset.setRolloverIcon(cre);
                        reset.setPressedIcon(cre);
                        reset.getModel().addChangeListener(new ChangeListener() {
                		        @Override
                		        public void stateChanged(ChangeEvent e) {
                		            ButtonModel model = (ButtonModel) e.getSource();
                		            if (model.isRollover()) {
                		            	reset.setIcon(cre);
                		            } else {
                		            	reset.setIcon(re);
                		            }// etc ...//  if (model.isPressed()) {
                		         }
                		    });
                        
                img=new ImageIcon("admin_student_background.png").getImage();
                setOpaque(false);
                panel.setOpaque(false);
                setLayout(new BorderLayout());
                add(panel,BorderLayout.PAGE_START);
               
                panel.setBackground(Color.WHITE);
                add(panel, BorderLayout.NORTH);
                panel.setPreferredSize(new Dimension(600,400));
               
                //-----TOP------
                panelt.setBorder(BorderFactory.createTitledBorder("Search Course:"));
                //would like to allow admin to search through db to quickly find student
                //just an idea, ill add code to implement this later if i can
                panelt.setBackground(Color.WHITE);
        panelt.setPreferredSize(new Dimension(600,80));
        panelt.add(fn);
                panelt.add(termText);
                panelt.add(ln);
                panelt.add(nameText);
                panelt.add(id);
                panelt.add(numberText);
                panelt.add(search);
                panelt.add(reset);
                panel.add(panelt, BorderLayout.NORTH);
       
       
       
               
                //-----CENTER PANEL----
        panelc.setBackground(Color.WHITE);
        panelc.setLayout(new BorderLayout());
        panelc.setPreferredSize(new Dimension(600,270));
       
        panelc1.setBackground(Color.WHITE);
        panelc1.setLayout(new BorderLayout());
        panelc.setBorder(BorderFactory.createTitledBorder("Remove/Edit Course:"));
       
        initialize();
       
        TableColumn column0;
        column0 = table.getColumnModel().getColumn(0);
        column0.setPreferredWidth(25);
               
                TableColumn column1;
        column1 = table.getColumnModel().getColumn(1);
        column1.setPreferredWidth(45);
       
        TableColumn column2;
        column2 = table.getColumnModel().getColumn(2);
        column2.setPreferredWidth(135);
       
        TableColumn column3;
        column3 = table.getColumnModel().getColumn(3);
        column3.setPreferredWidth(45);
               
        TableColumn column4;
        column4 = table.getColumnModel().getColumn(4);
        column4.setPreferredWidth(50);
       
        TableColumn column5;
        column5 = table.getColumnModel().getColumn(5);
        column5.setPreferredWidth(184);
       
        TableColumn column6;
        column6 = table.getColumnModel().getColumn(6);
        column6.setPreferredWidth(34);
       
        table.setFillsViewportHeight(true);
        JScrollPane j1 = new JScrollPane(table);
                j1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
               
                panelc1.setPreferredSize(new Dimension(600,120));
                panelc1.add(delete, BorderLayout.SOUTH);
                panelc1.add(j1, BorderLayout.CENTER);
                panelc.add(panelc1, BorderLayout.NORTH);
               
               
               
                panelc2.setBackground(Color.WHITE);
                panelc2.setPreferredSize(new Dimension(600,50));
                panelc2.setLayout(new BorderLayout());
                panelc2.setBorder(BorderFactory.createTitledBorder("Add Course:"));
               
               
                model2.addRow(data2[0]);
        model2.addRow(data2[1]);
        model2.addRow(data2[2]);
        model2.addRow(data2[3]);
        model2.addRow(data2[4]);
     
       
        table2 = new JTable(model2){
                public boolean isCellEditable(int row, int cols) {return true;}
               
        };
       
        TableColumn column00;
        column00 = table2.getColumnModel().getColumn(0);
        column00.setPreferredWidth(45);
               
                TableColumn column11;
        column11 = table2.getColumnModel().getColumn(1);
        column11.setPreferredWidth(135);
       
        TableColumn column22;
        column22 = table2.getColumnModel().getColumn(4);
        column22.setPreferredWidth(40);
       
        TableColumn column33;
        column33 = table2.getColumnModel().getColumn(5);
        column33.setPreferredWidth(40);
               
        TableColumn column44;
        column44 = table2.getColumnModel().getColumn(6);
        column44.setPreferredWidth(40);
       
        TableColumn column55;
        column55 = table2.getColumnModel().getColumn(7);
        column55.setPreferredWidth(40);
       
       
        table2.setFillsViewportHeight(true);
        JScrollPane j2 = new JScrollPane(table2);
                j2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
               
                panelc2.add(add, BorderLayout.SOUTH);
                panelc2.add(j2, BorderLayout.CENTER);
                panelc.add(panelc2, BorderLayout.CENTER);
                panel.add(panelc, BorderLayout.CENTER);
               
               
               
                //----BASE-----
                panelb.setBackground(Color.WHITE);
               // panelb.setPreferredSize(new Dimension(600,20));
                panelb.setLayout(new BorderLayout());
                panelb.add(home, BorderLayout.EAST);
                panel.add(panelb, BorderLayout.SOUTH);
                add(panel, BorderLayout.PAGE_START);
        panelb.setOpaque(false);
        
        final ImageIcon ho=new ImageIcon("home_button.png");
		final ImageIcon cho=new ImageIcon("home_cbutton.png");
		final ImageIcon ad=new ImageIcon("add_button.png");
		final ImageIcon cad=new ImageIcon("add_cbutton.png");
		final ImageIcon de=new ImageIcon("delete_button.png");
		final ImageIcon cde=new ImageIcon("delete_cbutton.png");
		button(home,ho);
		button(add,ad);
		button(delete,de);
       
                home.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
               
                remove(panel);
                add(new AdminMenuPanel(), BorderLayout.PAGE_START);
                validate();
                repaint();
               
            }
    });
                home.setContentAreaFilled(false);
                home.setIcon(ho);
                home.setRolloverIcon(cho);
                home.setPressedIcon(cho);
                home.getModel().addChangeListener(new ChangeListener() {
        		        @Override
        		        public void stateChanged(ChangeEvent e) {
        		            ButtonModel model = (ButtonModel) e.getSource();
        		            if (model.isRollover()) {
        		            	home.setIcon(cho);
        		            } else {
        		            	home.setIcon(ho);
        		            }// etc ...//  if (model.isPressed()) {
        		         }
        		    });
               
                delete.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                 
               // int c = row;
                for(int i =0; i<dtm.getRowCount(); i++){
                        String s= (String) table.getModel().getValueAt(i, 3);
                        Boolean b = (Boolean) table.getModel().getValueAt(i, 0);
                       
                        if(b==true)
                                {
                                                        System.out.println(s);
                                        dtm.removeRow(i);
                                        delete(s);
                                        i--;
                                       
                                }
                       
                }
            }
    });
                delete.setContentAreaFilled(false);
                delete.setIcon(de);
                delete.setRolloverIcon(cde);
                delete.setPressedIcon(cde);
                delete.getModel().addChangeListener(new ChangeListener() {
        		        @Override
        		        public void stateChanged(ChangeEvent e) {
        		            ButtonModel model = (ButtonModel) e.getSource();
        		            if (model.isRollover()) {
        		            	delete.setIcon(cde);
        		            } else {
        		            	delete.setIcon(de);
        		            }// etc ...//  if (model.isPressed()) {
        		         }
        		    });
               
                add.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                 
               
                for(int i =0; i<model2.getRowCount(); i++){
                       
                        String term = (String) table2.getModel().getValueAt(i, 0);
                        String name = (String) table2.getModel().getValueAt(i, 1);
                        String num = (String) table2.getModel().getValueAt(i,2);
                        String prof = (String) table2.getModel().getValueAt(i, 3);
                        String day = (String) table2.getModel().getValueAt(i, 4);
                        String st = (String) table2.getModel().getValueAt(i,5);
                        String et = (String) table2.getModel().getValueAt(i,6);
                        String c = (String) table2.getModel().getValueAt(i,7);
                       
                        if(term!=""&&c!=""&&name!=""&&num!=""&&prof!=""&&day!=""&&st!=""&&et!="")
                                {
                                System.out.println(term + " " + c + " " + num);      
                                        //insertrow(term, name,num,prof,day,st,et,c);
                                		dtm.addRow(new Object[]{j, term, name,num,prof,day,st,et,c});
                                        add(term,name,num,prof,day,st,et,c);
                                }
                }
                for(int j=0;j<model2.getRowCount();j++){
                	model2.removeRow(0);
                	model2.addRow(new Object[]{"","","",""});
                }
            }
    });
                add.setContentAreaFilled(false);
                add.setIcon(ad);
                add.setRolloverIcon(cad);
                add.setPressedIcon(cad);
                add.getModel().addChangeListener(new ChangeListener() {
        		        @Override
        		        public void stateChanged(ChangeEvent e) {
        		            ButtonModel model = (ButtonModel) e.getSource();
        		            if (model.isRollover()) {
        		            	add.setIcon(cad);
        		            } else {
        		            	add.setIcon(ad);
        		            }// etc ...//  if (model.isPressed()) {
        		         }
        		    });
        }
       
        void insertrow(String q,String w,String e, String r, String t, String y, String u, String i){
                int p2 = row + 1;
                dtm.addRow(new Object[]{j, q,w,e,r,t,y,u,i});
        }
       
        private void initialize(){
                dtm=new DefaultTableModel(data,cols){
                        public Class getColumnClass(int c) {
                    switch (c) {
                      case 0: return Boolean.class;
                      default: return String.class;
                    }  
                }
                         @Override
               public boolean isCellEditable(int row, int col) {
                    switch (col) {
                        case 0:
                            return true;
                        default:
                            return false;
                     }
               }
               
                };
                table=new JTable(dtm);
                //int row=0;
               
                try{
                        String dName = "com.mysql.jdbc.Driver";
                        String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                        Connection con = DriverManager.getConnection(url);
                        java.sql.Statement mysqlStatement = con.createStatement();
                        java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT cu.term, cu.start_time, cu.end_time, cu.days, cu.instructor, ca.course_name, ca.course_number, ca.credit_hours, cu.CID FROM next_semester_course_list AS cu JOIN course_catalog AS ca ON cu.CID = ca.CID  GROUP BY course_name");
                       
                        while (resultSet.next()) {
                                row+=1;
                                String term=resultSet.getString(1);
                                String start_time="";//=resultSet.getString(2);
                                String end_time="";//=resultSet.getString(3);
                                //String days;//=resultSet.getString(4);
                                String instructor=resultSet.getString(5);
                                String course_name=resultSet.getString(6);
                                String course_number=resultSet.getString(7);
                               
                                int credit_hours=resultSet.getInt(8);
                                String cid=resultSet.getString(9);
                                String days="";
                               
                                try{
                                        String dName2 = "com.mysql.jdbc.Driver";
                                        String url2 = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                                        Connection con2 = DriverManager.getConnection(url2);
                                        java.sql.Statement mysqlStatement2 = con2.createStatement();
                                        //java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT course_name, start_time, days From course_catalog, current_course_total_list WHERE course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
                                        java.sql.ResultSet resultSet2 = mysqlStatement2.executeQuery("SELECT co.start_time, co.end_time, co.days, co.CID From course_catalog AS ca JOIN next_semester_course_list AS co ON ca.CID = co.CID WHERE ca.CID ='"+cid+"' AND co.SID='"+MenuPanel.SID+"'");//start_time='"+start_time+"'");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
                                       
                                        //while (resultSet.next()&&resultSet2.next()) {
                                                while(resultSet2.next()){
                                                //String course_number= resultSet.getString(1);
                                                //String course_section=resultSet.getString(3);
                                                //course=course_number+"-"+course_name+"-"+course_section;
                                               
                                                String startTime=resultSet2.getString(1);
                                                String endTime=resultSet2.getString(2);
                                                String day=resultSet2.getString(3);
                                                start_time=startTime;
                                                end_time=endTime;
                                                crow+=1;
                                                //start_time+=startTime;
                                                //end_time+=endTime;
                                                days+=day;
                                               
                                                System.out.println(cid+": "+startTime+ " "+endTime+" "+days+" ");
                                               
                                               
                                        }
                                        con2.close();
                                        }
                                        catch(Exception ex){}
                                String dd=days+" "+start_time+":00 - "+end_time+":00";
                               
                                Object[] program={j,term,course_name,course_number,instructor,dd,credit_hours};
                                dtm.addRow(program);
                                System.out.println("added");
                        }
                        con.close();
                        }
                        catch(Exception ex){}
        }
       
        private void delete(String s){
                try{
                        String dName = "com.mysql.jdbc.Driver";
                        String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                        Connection con = DriverManager.getConnection(url);
                        java.sql.Statement mysqlStatement = con.createStatement();
                        mysqlStatement.executeUpdate("DELETE FROM next_semester_course_list WHERE CID IN (SELECT CID FROM course_catalog WHERE course_number='"+s+"')");
                        System.out.println(s+" "+"dropped?");
                        con.close();
                        }
                        catch(Exception ex){}
        }
       
        private void add(String term,String name,String num,String prof,String day,String st,String et,String c){
                int srow=row+40;
                try{
                        String dName = "com.mysql.jdbc.Driver";
                        String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                        Connection con = DriverManager.getConnection(url);
                        java.sql.Statement mysqlStatement = con.createStatement();
                        java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT COUNT(*) FROM next_semester_course_list WHERE CID IN (SELECT CID FROM course_catalog WHERE course_number='"+num+"')");
                       
                        while (resultSet.next()) {
                                int ocid=resultSet.getInt(1);
                               
                                System.out.println("cid already exist?");
                                if(ocid!=0){
                                System.out.println("course already exist");
                                }
                                else{
                                        try{
                                               
                                                String dName2 = "com.mysql.jdbc.Driver";
                                                String url2 = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
                                                Connection con2 = DriverManager.getConnection(url2);
                                                java.sql.Statement mysqlStatement2 = con2.createStatement();
                                                mysqlStatement2.executeUpdate("INSERT INTO next_semester_course_list VALUES ('"+(srow+1)+"','"+day+"','"+prof+"','','"+Integer.parseInt(et)+"','"+term+"','"+(srow+1)+"','"+Integer.parseInt(st)+"')");
                                                System.out.println("course inserted into next_semester course list");
                                               
                                                con2.close();
                                                }
                                                catch(Exception ex){}
                                }
                               
                        }
                        con.close();
                        }
                        catch(Exception ex){}
               
               
        }
       
        @Override
        public void paintComponent(Graphics g) {
                super.paintComponents(g);
            g.drawImage(img, 0, 0, this);
        }
       
        private void button(JButton b, ImageIcon i){
                b.setBorderPainted(false);
                b.setBorder(null);
                //button.setFocusable(false);
                b.setMargin(new Insets(0, 0, 0, 0));
                b.setContentAreaFilled(false);
                b.setIcon(i);
                //b.setRolloverIcon(myIcon2);
                //b.setPressedIcon(myIcon3);
        }
}
